package com.github.xuemingyu.springboot.acticitycloud.task;

import com.github.xuemingyu.springboot.acticitycloud.dao.ActivityDao;
import com.github.xuemingyu.springboot.acticitycloud.entity.Activity;
import com.github.xuemingyu.springboot.acticitycloud.entity.enumEntity.ActivityStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Component
public class UpdateActivityStatus {
    @Autowired
    private ActivityDao activityDao;

    /**
     * 定时更新活动状态
     */
    @Scheduled(cron = "0 0/1 * * * ? ")//每分钟执行一次
    public void dataPush() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = df.format(new Date());
        List<Activity> activityList = activityDao.findAllByDeleteTag(false);
        if (Objects.nonNull(activityList)&&activityList.size()>0) {
            activityList.forEach(q -> {
                if (q.getActivityEnd().compareTo(now) < 0) {
                    q.setActivityStatus(ActivityStatus.over);
                } else if (q.getActivityEnd().compareTo(now) >= 0 && q.getActivityStart().compareTo(now) <= 0) {
                    q.setActivityStatus(ActivityStatus.proceed);
                } else if (q.getSignEnd().compareTo(now) >= 0 && q.getSignStart().compareTo(now) <= 0) {
                    q.setActivityStatus(ActivityStatus.waitsign);
                }
                activityDao.save(q);
            });

        }
    }
}
